//
//  BTChooseCourseVC.m
//  BaxTer
//
//  Created by wsj2015 on 16/10/17.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "BTChooseCourseVC.h"

@interface BTChooseCourseVC ()
@property (weak, nonatomic) IBOutlet UIStackView *groupBg;
@property (weak, nonatomic) IBOutlet UIStackView *groupBg2;

@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UIButton *btn3;
@property (weak, nonatomic) IBOutlet UIButton *btn4;
@property (weak, nonatomic) IBOutlet UIButton *btn5;
@property (weak, nonatomic) IBOutlet UIButton *btn6;



@property (nonatomic,  assign) NSInteger select1;



@property (nonatomic,  assign) NSInteger select2;

@property (nonatomic,  strong) UIBarButtonItem *confirmBtn;

@property (nonatomic,  strong) UIButton *preBtn_forGroup2;

@property (nonatomic,  strong) BTCourseQueryTypeModel *conditionModel ;



@property (nonatomic, copy) void (^confirmBlock)(BTCourseQueryTypeModel *conditionModel );
@end

@implementation BTChooseCourseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"筛选";
    NSArray *arr = @[self.btn1,self.btn2,self.btn3,self.btn4,self.btn5,self.btn6];
    for (int i=0; i<arr.count; i ++) {
        UIButton *btn = arr[i];
        btn.tag = 100 + i;
        btn.layer.borderWidth = 1;
        btn.layer.borderColor = kBorderColor.CGColor;
        [btn setBackgroundColor:[UIColor whiteColor]];
        if (i== arr.count -1) {
            [btn setBackgroundColor:kMainColor];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
        if (self.isTopicCourse) {
            if (i>0 && i<4) {
                btn.enabled  =NO;
            }
        }
       
        
    }
    
    self.select1 = 5;
    self.select2 = 0;
    
   
//    
//    [self addObserver:self forKeyPath:@"select1" options:NSKeyValueObservingOptionNew context:nil];
//     [self addObserver:self forKeyPath:@"select2" options:NSKeyValueObservingOptionNew context:nil];
    
   
}
- (BTCourseQueryTypeModel *)conditionModel{
    if (!_conditionModel) {
        _conditionModel =[BTCourseQueryTypeModel new];
        if (self.isTopicCourse == YES) {
            _conditionModel.courseStatue = @(-1);
        }
        
    }
    return _conditionModel;
}

//- (void)dealloc{
//    [self removeObserver:self forKeyPath:@"select1"];
//     [self removeObserver:self forKeyPath:@"select2"];
//
//}
//- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
//    if ([keyPath isEqualToString:@"select1"] || [keyPath isEqualToString:@"select2"]  ) {
//        
//        if (self.select1 != -1) {
//            self.confirmBtn.enabled = YES;
//        }else{
//          self.confirmBtn.enabled = NO;
//        }
//        
//    }
//}
- (void)configUI{
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"确认" style:0 target:self action:@selector(confirm)];

    self.navigationItem.rightBarButtonItem = item;
 
    self.confirmBtn = item;
    
    UIView * view = self.groupBg2.subviews.firstObject;
    UIButton *btn = view.subviews.lastObject;
    [btn setTitleColor:kMainColor forState:UIControlStateNormal];
//    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //
    if (_isTopicCourse == NO) {
        return;
    }
//    NSArray<UIButton *> *arr = @[self.btn1,self.btn2,self.btn3,self.btn4,self.btn5];
    
    [self.groupBg2.subviews enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        if (obj.tag == 2 + 500 ) {
            UIButton *btn = obj.subviews.lastObject;
            [btn setTitle:@"点赞量(由高到低)" forState:UIControlStateNormal];
        }else if (obj.tag == 3 + 500){
         UIButton *btn = obj.subviews.lastObject;
             [btn setTitle:@"点赞量(由低到高)" forState:UIControlStateNormal];
            ;
        }
    }];
   

}
- (void)confirm{
    
    if (self.isTopicCourse) {
        //全部
        if ([self.conditionModel.courseStatue integerValue] == 6) {
            self.conditionModel.courseStatue = @(-1);
        }
        //
        if ([self.conditionModel.courseStatue integerValue] == 0) {
            self.conditionModel.courseStatue = @(0);
        }
    }
    
    if (self.confirmBlock) {
        self.confirmBlock(self.conditionModel);
    }
    [self.navigationController popViewControllerAnimated:YES];

}
+ (instancetype)showWithVC:(UINavigationController*)parent WithDoneBlock:(void (^)(BTCourseQueryTypeModel *conditionModel))doneBlock{
    BTChooseCourseVC *vc =[BTChooseCourseVC new];
    vc.confirmBlock  = doneBlock;
    vc.isTopicCourse = NO;
  
    [parent pushViewController:vc animated:YES];
    return vc;
    
}


- (IBAction)stateAction:(UIButton *)sender {
    NSInteger tag = sender.tag  -100;
    if (self.select1 == tag) {
        return;
    }
    NSArray<UIButton *> *arr = @[self.btn1,self.btn2,self.btn3,self.btn4,self.btn5,self.btn6];
    __block UIButton *preBtn ;
    [arr enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.tag == self.select1 + 100) {
            preBtn = obj;
            
        }
    }];
    [preBtn setBackgroundColor:[UIColor whiteColor]];
    [preBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [sender setBackgroundColor:kMainColor];
    [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    self.select1 = tag;
    
    if (self.isTopicCourse) {
        
        switch (self.select1) {
            case 0:
                 self.conditionModel.courseStatue = @(0);
                break;
            case 4:
                 self.conditionModel.courseStatue = @(1);
                break;
            case 5:
                 self.conditionModel.courseStatue = nil;
                break;
            default:
                break;
        }
        
    }else{
    
         self.conditionModel.courseStatue = @(self.select1);
        if (self.select1 ==5) {
            self.conditionModel.courseStatue  =nil;
        }
    }
   

    
}

- (IBAction)conditionAction:(UIButton *)sender {
    NSInteger tag = sender.tag  -200;
    if (tag == self.select2) {
        return;
    }
    if (self.select2 == -1) {
        
    }else{
        UIView *view = [self.groupBg2 viewWithTag:self.select2 + 500];
        UIButton *preBtn = view.subviews.lastObject;
        [preBtn setBackgroundColor:[UIColor whiteColor]];
        [preBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
//    [sender setBackgroundColor:kMainColor];
    [sender setTitleColor:kMainColor forState:UIControlStateNormal];

   
    self.select2 = tag;
    
    switch (self.select2) {
        case 0:
            self.conditionModel.condition = @"updatedate";
             self.conditionModel.sortType =@"desc";
            break;
        case 1:
            self.conditionModel.condition = @"updatedate";
            self.conditionModel.sortType =@"asc";
            
            break;
        case 2:
            if (self.isTopicCourse == YES) {
                self.conditionModel.condition = @"topiclikecount";
                self.conditionModel.sortType =@"desc";
            }else{
                self.conditionModel.condition = @"courseScore";
                self.conditionModel.sortType =@"desc";
            
            }
            
            break;
        case 3:
            if (self.isTopicCourse == YES) {
                self.conditionModel.condition = @"topiclikecount";
                self.conditionModel.sortType =@"asc";
            }else{
                self.conditionModel.condition = @"courseScore";
                self.conditionModel.sortType =@"asc";
            }
          
            break;
        case 4:
            
            if (self.isTopicCourse == YES) {
                self.conditionModel.condition = @"topicclickcount";
                self.conditionModel.sortType =@"desc";
            }else{
                self.conditionModel.condition = @"viewNum";
                self.conditionModel.sortType = @"desc";
            }
            
         
            break;
        case 5:
            if (self.isTopicCourse == YES) {
                self.conditionModel.condition = @"topicclickcount";
                self.conditionModel.sortType = @"asc";
            }else{
                self.conditionModel.condition = @"viewNum";
                self.conditionModel.sortType =  @"asc";
            }
      
            break;
            
        default:
            break;
    }
    
}
- (void)setIsTopicCourse:(BOOL)isTopicCourse{
    _isTopicCourse = isTopicCourse;
    
   
    
}

@end
